package com.bnyy.medicalHousekeeper.moudle.wallet.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.bnyy.common.Constant;
import com.bnyy.gbp.CashierInputFilter;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.App;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.bean.MineInfo;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.wallet.bean.BankCardInfo;
import com.kevin.photo_browse.PhotoBrowse;
import com.kevin.photo_browse.ShowType;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class WithdrawalActivity extends BaseActivityImpl {
    @BindView(R.id.tv_bank_card)
    TextView tvBankCard;
    @BindView(R.id.et_withdrawal_count)
    EditText etWithdrawalCount;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.tv_add_bank_card)
    TextView tvAddBankCard;
    @BindView(R.id.ll_change_bank_card)
    LinearLayout llChangeBankCard;

    private float balance;

    @Override
    public int getLayoutId() {
        return R.layout.activity_withdrawal;
    }

    @Override
    public String getTitleStr() {
        return "提现";
    }

//    public static void show(Activity context, float balance, int requestCode) {
//        Intent intent = new Intent(context, WithdrawalActivity.class);
//        intent.putExtra("balance", balance);
//        context.startActivityForResult(intent, requestCode);
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //设置字符过滤
        etWithdrawalCount.setFilters(new InputFilter[]{new CashierInputFilter()});

        requestManager.request(requestManager.mJavaRetrofitService.getBankCardList(),
                new BaseObserverImpl<ArrayList<BankCardInfo>>() {
                    @Override
                    public void onSuccess(ArrayList<BankCardInfo> bankCardInfos) {
                        super.onSuccess(bankCardInfos);
                        if (bankCardInfos.size() > 0) {
                            tvAddBankCard.setVisibility(View.GONE);
                            llChangeBankCard.setVisibility(View.VISIBLE);
                            BankCardInfo bankCardInfo = bankCardInfos.get(0);
                            tvBankCard.setText(bankCardInfo.getBank_name() + "  " + bankCardInfo.getBank_card_no().substring(bankCardInfo.getBank_card_no().length() - 4));
                            tvBankCard.setTag(bankCardInfo);
                        }
                    }
                });
        getBalance();
    }

    private void getBalance() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", App.getUser().getUserInfo().getId());
        requestManager.request(requestManager.mJavaRetrofitService.getMineInfo(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<MineInfo>(mContext) {
                    @Override
                    public void onSuccess(MineInfo mineInfo) {
                        super.onSuccess(mineInfo);
                        balance = mineInfo.getWithdrawal_money();
                        SpanUtils.with(tvBalance)
                                .append("当前余额为：").setForegroundColor(ContextCompat.getColor(mContext, R.color.gray_light))
                                .append("¥" + mineInfo.getWithdrawal_money())
                                .create();
                    }
                });
    }

    @OnClick({
            R.id.tv_change_bank_card,
            R.id.tv_withdrawal_all,
            R.id.tv_confirm,
            R.id.tv_add_bank_card
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add_bank_card:
            case R.id.tv_change_bank_card:
                Intent intent = new Intent(mContext, BankCardListActivity.class);
                startActivityForResult(intent, Constant.ActivityRequestCode.SELECT_BANK_CARD);
                break;
            case R.id.tv_withdrawal_all:
                if (balance <= 0) {
                    Toast.makeText(mContext, "提现金额不能低于1元", Toast.LENGTH_SHORT).show();
                } else {
                    etWithdrawalCount.setText(String.format("%.2f", balance));
                    etWithdrawalCount.setSelection(etWithdrawalCount.getText().length() - 1);
                }
                break;
            case R.id.tv_confirm:
                BankCardInfo bankCardInfo = (BankCardInfo) tvBankCard.getTag();
                if (bankCardInfo == null) {
                    Toast.makeText(mContext, "请先绑定银行卡再提现", Toast.LENGTH_SHORT).show();
                    return;
                }
                String withdrawalCount = etWithdrawalCount.getText().toString();
                if (TextUtils.isEmpty(withdrawalCount)) {
                    Toast.makeText(mContext, "请输入提现金额", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (Float.parseFloat(withdrawalCount) <= 0) {
                    Toast.makeText(mContext, "提现金额不能低于1元", Toast.LENGTH_SHORT).show();
                    return;
                }
                HashMap<String, Object> params = new HashMap<>();
                params.put("bank_card_id", bankCardInfo.getId());
                params.put("money", withdrawalCount);
                requestManager.request(requestManager.mJavaRetrofitService.withDrawal(RequestManager.getJsonRequestBody(params)),
                        new BaseObserverImpl<Object>(mContext) {
                            @Override
                            public void onSuccess() {
                                super.onSuccess();

                                getBalance();
                            }
                        });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ActivityRequestCode.SELECT_BANK_CARD && resultCode == RESULT_OK) {
            BankCardInfo bankCardInfo = (BankCardInfo) data.getSerializableExtra("bankCardInfo");
            tvAddBankCard.setVisibility(View.GONE);
            llChangeBankCard.setVisibility(View.VISIBLE);
            tvBankCard.setTag(bankCardInfo);
            tvBankCard.setText(bankCardInfo.getBank_name() + "  " + bankCardInfo.getBank_card_no().substring(bankCardInfo.getBank_card_no().length() - 4));
        }
    }
}















